﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Code4WP7SDK;
using LuWeibo.Models;
using System.Text;
using LuWeiboSDK;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Diagnostics;

namespace LuWeibo.ViewModels
{
    public class UserInfoViewModel : BaseModel
    {
        #region Feilds
        private User _currentUser;                              //当前用户
        private ObservableCollection<User> _friendShipUsers;    //关注或者粉丝列表
        private ObservableCollection<Status> _currentUserStatus;//当前用户微博列表
        private ICommand _searchUserCommand;                    //查询用户的Command
        private ObservableCollection<Trend> _trends; //获取当期用户的话题
        private int _trendsCount;   //获取话题数目
        private ICommand _goToStatusDetailCommand;
        private Status _selectedStatus;
        #endregion

        #region Properties
        public User CurrentUser
        {
            get { return _currentUser; }
            set
            {
                _currentUser = value;
                this.NotifyPropertyChanged(p => p.CurrentUser);
            }
        }

        public ObservableCollection<User> FriendShipUsers
        {
            get
            {
                if (this._friendShipUsers == null)
                {
                    this._friendShipUsers = new ObservableCollection<User>();
                }
                return _friendShipUsers;
            }
            set
            {
                _friendShipUsers = value;
                this.NotifyPropertyChanged(p => p.FriendShipUsers);
            }
        }

        public ObservableCollection<Status> CurrentUserStatus
        {
            get
            {
                if (this._currentUserStatus == null)
                {
                    this._currentUserStatus = new ObservableCollection<Status>();
                }
                return _currentUserStatus;
            }
            set
            {
                _currentUserStatus = value;
                this.NotifyPropertyChanged(p => p.CurrentUserStatus);
            }
        }

        public ObservableCollection<Trend> Trends
        {
            get
            {
                if (this._trends == null)
                {
                    this._trends = new ObservableCollection<Trend>();
                }
                return _trends;
            }
            set
            {
                _trends = value;
                this.NotifyPropertyChanged(p => p.Trends);
            }
        }

        public int TrendsCount
        {
            get { return _trendsCount; }
            set
            {
                _trendsCount = value;
                this.NotifyPropertyChanged(p => p.TrendsCount);
            }
        }

        public Status SelectedStatus
        {
            get { return _selectedStatus; }
            set
            {
                _selectedStatus = value;
                this.NotifyPropertyChanged(p => p.SelectedStatus);
            }
        }
        public Int64 UID { get; set; }

        #endregion

        #region Command
        public ICommand SearchUserCommand
        {
            get
            {
                if (this._searchUserCommand == null)
                {
                    this._searchUserCommand = new BaseCommand(SearchUserExecute);
                }
                return _searchUserCommand;
            }
        }

        public ICommand GoToStatusDetailCommand
        {
            get
            {
                if (this._goToStatusDetailCommand == null)
                {
                    this._goToStatusDetailCommand = new BaseCommand<Status>(GoToStatusDetailExecute);
                }
                return _goToStatusDetailCommand;
            }
        }
        #endregion

        #region Method
        public void SearchUserExecute()
        {
            List<Parameter> paras = new List<Parameter>();
            if (this.UID == 0)//没有传递uid过来，则说明要先当前登录用户
            {
                paras.Add(new Parameter("uid", Constant.UID));
            }
            else
            {
                paras.Add(new Parameter("uid", this.UID));
            }
            string data = ParameterHelper.BuildParameters(paras);
            WebClientService ws = new WebClientService();
            ws.WebClientCompleted += (s, e) =>
            {
                if (e.Ex == null)
                {
                    this.CurrentUser = JsonConvert.DeserializeObject<User>(e.Result.ToString());
                }
                else
                {
                    //出错了
                }

            };
            ws.GetRequest(Urls.users_show, data);
        }

        public void GetFriendShip()
        {
            this.FriendShipUsers.Clear();//清楚粉丝用户列表
            WebClientService ws = new WebClientService();
            List<Parameter> paras = new List<Parameter>();
            if (this.UID == 0)
            {
                paras.Add(new Parameter("uid", Constant.UID));
            }
            else
            {
                paras.Add(new Parameter("uid", this.UID));
            }
            string data = ParameterHelper.BuildParameters(paras);
            ws.WebClientCompleted += (s, e) =>
            {
                if (e.Ex == null)
                {
                    using (JsonHelper jhelper = new JsonHelper(e.Result.ToString()))
                    {
                        this.FriendShipUsers = jhelper.get<ObservableCollection<User>>("users");
                    }
                }
                else
                {
                    //获取用户列表失败
                }
            };
            ws.GetRequest(Urls.friendships_friends, data);
        }

        public void GetFans()
        {
            this.FriendShipUsers.Clear();//清楚关注用户列表
            WebClientService ws = new WebClientService();
            List<Parameter> paras = new List<Parameter>();
            if (this.UID == 0)
            {
                paras.Add(new Parameter("uid", Constant.UID));
            }
            else
            {
                paras.Add(new Parameter("uid", this.UID));
            }
            string data = ParameterHelper.BuildParameters(paras);
            ws.WebClientCompleted += (s, e) =>
            {
                if (e.Ex == null)
                {
                    using (JsonHelper jhelper = new JsonHelper(e.Result.ToString()))
                    {
                        this.FriendShipUsers = jhelper.get<ObservableCollection<User>>("users");
                    }
                }
                else
                {
                    //获取用户列表失败
                }
            };
            ws.GetRequest(Urls.friendships_followers, data);
        }

        public void GetCurrentUserStatus()
        {
            this.CurrentUserStatus.Clear();
            WebClientService ws = new WebClientService();
            List<Parameter> paras = new List<Parameter>();
            if (this.UID == 0)
            {
                paras.Add(new Parameter("uid", Constant.UID));
            }
            else
            {
                paras.Add(new Parameter("uid", this.UID));
            }
            string data = ParameterHelper.BuildParameters(paras);
            ws.WebClientCompleted += (s, e) =>
            {
                if (e.Ex == null)
                {
                    using (JsonHelper jhelper = new JsonHelper(e.Result.ToString()))
                    {
                        this.CurrentUserStatus = jhelper.get<ObservableCollection<Status>>("statuses");
                    }
                }
                else
                {
                    //获取当前用户微博信息失败
                }
            };
            ws.GetRequest(Urls.statuses_user_timeline, data);
        }

        public void GetTrendsList()
        {
            this.Trends.Clear();
            WebClientService ws = new WebClientService();
            List<Parameter> paras = new List<Parameter>();
            if (this.UID == 0)
            {
                paras.Add(new Parameter("uid", Constant.UID));
            }
            else
            {
                paras.Add(new Parameter("uid", UID));
            }
            string data = ParameterHelper.BuildParameters(paras);
            ws.WebClientCompleted += (s, e) =>
            {
                if (e.Ex == null)
                {
                    using (JsonHelper jhelper = new JsonHelper(e.Result.ToString()))
                    {
                        this.Trends = JsonConvert.DeserializeObject<ObservableCollection<Trend>>(e.Result.ToString());
                        this.TrendsCount = this.Trends.Count;
                    }
                }
                else
                {
                    //获取当前用户的收藏列表失败
                }
            };
            ws.GetRequest(Urls.user_trends, data);
        }

        public void GoToStatusDetailExecute(Status selectedStatus)
        {
            if (selectedStatus != null)
            {
                ViewModelLocator.StatusDetailVMStatic.CurrentStatus = selectedStatus;
                VmNavigateHelper.Navigate("/Views/Profile/UsersStatusPage.xaml", "/Views/Status/StatusDeatailPage.xaml");
                this.SelectedStatus = null;
            }
        }
        #endregion
    }
}
